#ifndef TASKMODEL_H
#define TASKMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QDateTime>
#include <qjsonarray.h>
#include <qjsonobject.h>

struct Task {
    QString name;
    int executeCount;
    int dailyPoints[7];
    int dailyStatus[7];

    Task(const QString& taskName = "", int count = 0)
        : name(taskName), executeCount(count) {
        for(int i = 0; i < 7; ++i) {
            dailyPoints[i] = 0;
            dailyStatus[i] = 0;
        }
    }

    Task(const QString& taskName, int count, int points[])
        : name(taskName), executeCount(count) {
        for(int i = 0; i < 7; ++i) {
            dailyPoints[i] = points[i];
            dailyStatus[i] = 0;
            qDebug() << "i " << points[i];
        }
    }

    // 新增：从JSON对象构造任务
    Task(const QJsonObject &jsonObj) {
        name = jsonObj["name"].toString("");
        executeCount = jsonObj["executeCount"].toInt(0);

        QJsonArray pointsArray = jsonObj["dayPoints"].toArray();
        for(int i = 0; i < 7 && i < pointsArray.size(); ++i) {
            dailyPoints[i] = pointsArray[i].toInt(0);
            dailyStatus[i] = 0;
        }
        // 如果数组长度不足7，用0填充剩余位置
        for(int i = pointsArray.size(); i < 7; ++i) {
            dailyPoints[i] = 0;
            dailyStatus[i] = 0;
        }
    }

    // 新增：将任务转换为JSON对象
    QJsonObject toJson() const {
        QJsonObject jsonObj;
        jsonObj["name"] = name;
        jsonObj["executeCount"] = executeCount;

        QJsonArray pointsArray;
        for(int i = 0; i < 7; ++i) {
            pointsArray.append(dailyPoints[i]);
        }
        jsonObj["dayPoints"] = pointsArray;

        return jsonObj;
    }
};

class TaskModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum RoleNames {
        NameRole = Qt::UserRole + 1,
        ExecuteCountRole,
        DayPointsRole
    };

    explicit TaskModel(QObject *parent = nullptr);

    // QAbstractItemModel interface
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QHash<int, QByteArray> roleNames() const override;

    // 管理任务的自定义方法
    Q_INVOKABLE void addTask(const QString& name, int executeCount, const QVariantList &points);
    Q_INVOKABLE void removeTask(int index);
    Q_INVOKABLE void updateTask(int index, const QString& name, int executeCount);
    Q_INVOKABLE void updateDayPoints(int taskIndex, int dayIndex, int points);
    Q_INVOKABLE QVariantList getDayPoints(int taskIndex) const;
    Q_INVOKABLE void clearAll();

    Q_INVOKABLE void completeTask(int taskIndex, int day);

    // 新增：JSON存储功能
    Q_INVOKABLE void saveTaskData();
    Q_INVOKABLE void loadTaskData();

private:
    QList<Task> m_taskList;
    QHash<int, QByteArray> m_roleNames;

    // 新增：JSON存储相关方法
    QString getDataFilePath() const;
    void createInitialJsonFile();
    bool loadFromJsonFile();
};

#endif // TASKMODEL_H
